{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Interactive Average\n",
    "===================\n",
    "The plot below uses an interval selection, which causes the chart to include an interactive brush\n",
    "(shown in grey). The brush selection parameterizes the red guideline, which visualizes the average\n",
    "value within the selected interval.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# category: interactive\n",
    "\n",
    "import altair as alt\n",
    "alt.data_transformers.enable('json')\n",
    "from vega_datasets import data\n",
    "\n",
    "weather = data.seattle_weather.url\n",
    "brush = alt.selection(type='interval', encodings=['x'])\n",
    "\n",
    "bars = alt.Chart().mark_bar().encode(\n",
    "    alt.X('date:O', timeUnit='month'),\n",
    "    y='mean(precipitation):Q',\n",
    "    opacity=alt.condition(brush, alt.OpacityValue(1), alt.OpacityValue(0.7))\n",
    ").properties(\n",
    "    selection=brush\n",
    ")\n",
    "\n",
    "line = alt.Chart().mark_rule(color='firebrick').encode(\n",
    "    y='mean(precipitation):Q',\n",
    "    size=alt.SizeValue(3)\n",
    ").transform_filter(brush.ref())\n",
    "\n",
    "alt.layer(bars, line, data=weather)\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "env": {},
   "interrupt_mode": "signal",
   "language": "python",
   "metadata": {},
   "name": "python3"
  },
  "language": "python"
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
